package cn.sdjzu.exercise;

import cn.sdjzu.exercise.db.DBHelper;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CustomizeModeActivity extends BasedActivity implements
		OnClickListener {

	private EditText mWeight;
	private EditText mInitWeight;
	private EditText mGroups;
	private EditText mTimes;
	private EditText mPushTime;
	private EditText mHoldTime;
	private EditText mResetTime;
	private EditText mSleepTime;

	private Button mStartBtn;
	private Button mBackBtn;

	private String weightStr;
	private String initweightStr;
	private String groupsStr;
	private String timesStr;
	private String pushTimeStr;
	private String holdTimeStr;
	private String resetTimeStr;
	private String sleepTimeStr;
	
	ExerciseApp app;
	private DBHelper dbHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExerciseApp.getInstance().addActivity(this);
		setContentView(R.layout.customize_mode);
		app = (ExerciseApp) CustomizeModeActivity.this.getApplication();

		mWeight = (EditText) findViewById(R.id.customize_weight);
		mInitWeight = (EditText) findViewById(R.id.customize_init_weight);

		dbHelper = new DBHelper(this);
		int maxforce;
		try {
			maxforce = Integer.parseInt(dbHelper.getDataFromDB(
					DBHelper.TBL_USER_EXT, DBHelper.TBL_USER_EXT_MAXFORCE,
					app.currentCardNo));
		} catch (Exception e) {
			maxforce = 100;
		}
		mWeight.setText(maxforce / 2 + "");
		mInitWeight.setText(maxforce / 2 + "");

		String times = dbHelper.getDataFromDB(DBHelper.TBL_USER_EXT,
				DBHelper.TBL_USER_EXT_TIMES, app.currentCardNo);
		if ("".equals(times)) {
			times = "5";
		}
		String groups = dbHelper.getDataFromDB(DBHelper.TBL_USER_EXT,
				DBHelper.TBL_USER_EXT_GROUPS, app.currentCardNo);
		if ("".equals(groups)) {
			groups = "5";
		}
		mGroups = (EditText) findViewById(R.id.customize_groups);
		mGroups.setText(times);
		mTimes = (EditText) findViewById(R.id.customize_times);
		mTimes.setText(times);
		mPushTime = (EditText) findViewById(R.id.customize_pushtime);
		mHoldTime = (EditText) findViewById(R.id.customize_holdtime);
		mResetTime = (EditText) findViewById(R.id.customize_resettime);
		mSleepTime = (EditText) findViewById(R.id.customize_sleeptime);

		String pushtime = dbHelper.getDataFromDB(DBHelper.TBL_USER_EXT,
				DBHelper.TBL_USER_EXT_PUSHTIME, app.currentCardNo);
		if ("".equals(pushtime)) {
			pushtime = "2";
		}
		mPushTime.setText(pushtime);
		
		String holdtime = dbHelper.getDataFromDB(DBHelper.TBL_USER_EXT,
				DBHelper.TBL_USER_EXT_HOLDTIME, app.currentCardNo);
		if ("".equals(holdtime)) {
			holdtime = "1";
		}
		mHoldTime.setText(holdtime);

		
		String resettime = dbHelper.getDataFromDB(DBHelper.TBL_USER_EXT,
				DBHelper.TBL_USER_EXT_RESETTIME, app.currentCardNo);
		if ("".equals(resettime)) {
			resettime = "2.5";
		}
		mResetTime.setText(resettime);

		String sleeptime = dbHelper.getDataFromDB(DBHelper.TBL_USER_EXT,
				DBHelper.TBL_USER_EXT_SLEEPTIME, app.currentCardNo);
		if ("".equals(sleeptime)) {
			sleeptime = "1";
		}
		mSleepTime.setText(sleeptime);

        dbHelper.close();
        dbHelper = null;
        
		mStartBtn = (Button) findViewById(R.id.customize_btn_start);
		mBackBtn = (Button) findViewById(R.id.customize_btn_back);

		mStartBtn.setOnClickListener(this);
		mBackBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.customize_btn_start:
			weightStr = mWeight.getText().toString().trim();
			initweightStr = mInitWeight.getText().toString().trim();
			groupsStr= mGroups.getText().toString().trim();
			timesStr = mTimes.getText().toString().trim();
			pushTimeStr = mPushTime.getText().toString().trim();
			holdTimeStr = mHoldTime.getText().toString().trim();
			resetTimeStr = mResetTime.getText().toString().trim();
			sleepTimeStr = mSleepTime.getText().toString().trim();
			boolean flg = checkInput();
			if (flg) {
				ContentValues values = new ContentValues();
				values.put(DBHelper.TBL_USER_EXT_CARDNO, app.currentCardNo);
				values.put(DBHelper.TBL_USER_EXT_GROUPS, groupsStr);
				values.put(DBHelper.TBL_USER_EXT_TIMES, timesStr);
				values.put(DBHelper.TBL_USER_EXT_PUSHTIME, pushTimeStr);
				values.put(DBHelper.TBL_USER_EXT_HOLDTIME, holdTimeStr);
				values.put(DBHelper.TBL_USER_EXT_RESETTIME, resetTimeStr);
				values.put(DBHelper.TBL_USER_EXT_SLEEPTIME, sleepTimeStr);
				dbHelper = new DBHelper(this);
				if (dbHelper.isExistCardNoInDB(DBHelper.TBL_USER_EXT, app.currentCardNo)) {
					dbHelper.updateUserExtData(values);
				} else {					
					dbHelper.insertData2DB(DBHelper.TBL_USER_EXT, values);
				}
				dbHelper.close();
				dbHelper = null;
				app.mPushTime = pushTimeStr;
				app.mHoldTime = holdTimeStr;
				app.mResetTime = resetTimeStr;
				app.mSleepTime = sleepTimeStr;	
				
				float[] custome ={Float.parseFloat(weightStr),
						Float.parseFloat(initweightStr),
						Float.parseFloat(groupsStr),
						Float.parseFloat(timesStr),
						Float.parseFloat(pushTimeStr),
						Float.parseFloat(holdTimeStr),
						Float.parseFloat(resetTimeStr),
						Float.parseFloat(sleepTimeStr)};
				
				Intent intent = new Intent(this, TestLineSaveActivity.class);//ExerciseActivity.class);
				ExerciseActivity.currentExerciseMode = MODE_CUSTOMIZE;
				intent.putExtra("custome", custome);
				startActivity(intent);
				overridePendingTransition(android.R.anim.fade_in,
						android.R.anim.fade_out);
			}
			break;

		case R.id.customize_btn_back:
			Intent intent = new Intent(this, ControlPanelActivity.class);
			startActivity(intent);
			this.finish();
			break;
		default:
			break;
		}
	}

	private boolean checkInput() {

		if ("".equals(weightStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mWeight.requestFocus();
			return false;
		}
		if ("".equals(initweightStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mInitWeight.requestFocus();
			return false;
		}
		if ("".equals(groupsStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mGroups.requestFocus();
			return false;
		}
		if ("".equals(timesStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mTimes.requestFocus();
			return false;
		}
		if ("".equals(pushTimeStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mPushTime.requestFocus();
			return false;
		}
		if ("".equals(holdTimeStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mHoldTime.requestFocus();
			return false;
		}
		if ("".equals(resetTimeStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mResetTime.requestFocus();
			return false;
		}
		if ("".equals(sleepTimeStr)) {
			Toast.makeText(this, R.string.customize_input_error,
					Toast.LENGTH_SHORT).show();
			mSleepTime.requestFocus();
			return false;
		}
		return true;
	}

	@Override
	public void onResume() {
		super.onResume();
		LayoutParams mWeightlp, mInitWeightlp, mGroupslp, mTimeslp, mPushTimelp, mHoldTimelp, mResetTimelp, mSleepTimelp;
		mWeightlp = mWeight.getLayoutParams();
		mInitWeightlp = mInitWeight.getLayoutParams();
		mGroupslp = mGroups.getLayoutParams();
		mTimeslp = mTimes.getLayoutParams();
		mPushTimelp = mPushTime.getLayoutParams();
		mHoldTimelp = mHoldTime.getLayoutParams();
		mResetTimelp = mResetTime.getLayoutParams();
		mSleepTimelp = mSleepTime.getLayoutParams();
		mWeightlp.height = EditH;
		mInitWeightlp.height = EditH;
		mGroupslp.height = EditH;
		mTimeslp.height = EditH;
		mPushTimelp.height = EditH;
		mHoldTimelp.height = EditH;
		mResetTimelp.height = EditH;
		mSleepTimelp.height = EditH;
		mWeight.setLayoutParams(mWeightlp);
		mInitWeight.setLayoutParams(mInitWeightlp);
		mGroups.setLayoutParams(mGroupslp);
		mTimes.setLayoutParams(mTimeslp);
		mPushTime.setLayoutParams(mPushTimelp);
		mHoldTime.setLayoutParams(mHoldTimelp);
		mResetTime.setLayoutParams(mResetTimelp);
		mSleepTime.setLayoutParams(mSleepTimelp);
	
	}
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, ControlPanelActivity.class);
		startActivity(intent);
		this.finish();
	}

}
